package com.yztc.orangeentertainment.ui.main.ArticleShouCang;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.ShouCangAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.channalbean.InfoBean;
import com.yztc.orangeentertainment.bean.shoucang.ShouCang;
import com.yztc.orangeentertainment.ui.main.channalarticle.ChannalArticleActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/16.
 */
public class ArticleShouCangActivity extends BaseActivity implements ArticleShouCangContract.View {

    @InjectView(R.id.article_shoucangback)
    ImageButton articleShoucangback;
    @InjectView(R.id.tv_shoucang1)
    TextView tvShoucang1;
    @InjectView(R.id.article_shoucang)
    RecyclerView articleShoucang;
    @InjectView(R.id.sr_shoucang)
    SwipeRefreshLayout srShoucang;
    private ShouCangAdapter adapter;
    private List<ShouCang> shoucangs=new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.artilceshoucang;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //垂直摆放
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //间距
        articleShoucang.addItemDecoration(new DividerItemDecoration(5));
        articleShoucang.setLayoutManager(linearLayoutManager);
        initdata();
        //下拉刷新
        srShoucang.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initdata();
                        adapter.notifyDataSetChanged();
                        srShoucang.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //adapter中点击每一项item传值跳转
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(ArticleShouCangActivity.this, ChannalArticleActivity
                        .class);
                intent.putExtra("id",shoucangs.get(position).getId()+"");
                intent.putExtra("readnum",shoucangs.get(position).getReadnum()+"");
                intent.putExtra("iconimage",shoucangs.get(position).getIconimage());
                startActivity(intent);
            }
            //长按弹出删除对话框
            @Override
            public void onItemLongClick(View view, final int position) {
                builder.setMessage("真的要删除吗").setPositiveButton("是", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int num=App.getLiteOrm().delete(new com.litesuits.orm.db.assit.WhereBuilder(ShouCang.class).where
                                ("id"+"=?",new Object[]{shoucangs.get(position).getId()+""}));
                        Log.i("log","--------------"+num);
                        if(num>0){

                            initdata();
                        }
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                });
                AlertDialog ad  = builder.create();
                ad.show();
            }
        });
        articleShoucangback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    //从数据库中获取数据
    private void initdata() {
        shoucangs=App.getLiteOrm().query(ShouCang.class);
        adapter = new ShouCangAdapter(shoucangs,this);
        articleShoucang.setAdapter(adapter);
    }

    //数据获取成功
    @Override
    public void onArticleShouCangSucess(List<InfoBean> infoBeanList) {

    }
    //数据获取失败
    @Override
    public void onArticleShouCangFaild(String msg) {

    }
}
